package com.obas.business.topo {

//	import com.pktech.nip.platform.model.term.TopoView;
//	import com.pktech.topo.lib.constants.TopoLibConstants;
//	import com.pktech.topo.lib.events.NetworkEvent;
//	import com.pktech.topo.lib.views.TopoMainView;
//	import com.pktech.topo.lib.views.components.editor.ElementCreator;
//	import com.pktech.topo.lib.views.components.editor.IElementCreator;
//	import com.pktech.topo.lib.views.components.editor.IStylesCreator;
//	import com.pktech.topo.lib.views.components.element.TopoViewData;
//	import com.pktech.topo.lib.views.components.element.TopoViewEntity;
//	import com.pktech.topo.lib.views.components.element.TopoViewLink;
//	import com.pktech.topo.lib.views.components.element.TopoViewNode;
	
	import mx.events.DragEvent;
	import mx.managers.DragManager;
	
	import twaver.Consts;
	import twaver.Element;
	import twaver.ElementBox;
	import twaver.IElement;
	import twaver.ISubNetwork;
	import twaver.Link;
	import twaver.LinkSubNetwork;
	import twaver.Node;
	import twaver.Styles;
	import twaver.network.Network;
	import twaver.network.interaction.InteractionEvent;
	import twaver.network.layout.AutoLayouter;

	public class TopoNetwork extends BaseNetwork {
		public var autoLayouter:AutoLayouter;
		private var _dataVisible:int = 1;
		/**
		 * 自动布局常量
		 */
		private var autoLayouterData:Array=[
			{label: "向左对齐", value: Consts.LAYOUT_LEFTRIGHT , index:1}, 
			{label: "向右对齐", value: Consts.LAYOUT_RIGHTLEFT , index:2}, 
			{label: "向上对齐", value: Consts.LAYOUT_TOPBOTTOM , index:3}, 	
			{label: "向下对齐", value: Consts.LAYOUT_BOTTOMTOP , index:4},
			{label: "环状布局", value: Consts.LAYOUT_ROUND , index:5}, 
			{label: "对称布局", value: Consts.LAYOUT_SYMMETRY , index:6}
		]
		
		public function TopoNetwork(elementBox:ElementBox=null) {
			super(elementBox);
			setNetwrokStyle()
			autoLayouter =new AutoLayouter(this);
			
			autoLayouter.explicitXOffset = 100;
			autoLayouter.explicitYOffset = 20;
			
//			this.addEventListener(DragEvent.DRAG_ENTER,onDragEnter);
			this.addEventListener(DragEvent.DRAG_DROP,onDragDrop);
			this.addEventListener(DragEvent.DRAG_OVER,onDragOver);
			//network上注册监听事件
			this.addInteractionListener(handleInteraction);
         
//			TopoMainView.addEventListener(NetworkEvent.EDIT_CLICK , onEditClickEvent);
			this.visibleFunction = elementVisible;
		}
		
		/**
		 * 设置TopoView
		 */
//		public function setTopoView(topoView:TopoView):void{
//			var currentSubNetwork:ISubNetwork  = this.currentSubNetwork;
//			if(null != currentSubNetwork){
//				currentSubNetwork.setClient(TopoViewEntity.TOPO_VIEW , topoView);
//			}else{
//				this.elementBox.setClient(TopoViewEntity.TOPO_VIEW , topoView);
//			}
//		}
		
//		public function getTopoView():TopoView{
//			var topoView:TopoView;
//			var currentSubNetwork:ISubNetwork  = this.currentSubNetwork;
//			if(null != currentSubNetwork){
//				topoView = currentSubNetwork.getClient(TopoViewEntity.TOPO_VIEW);
//			}else{
//				topoView = this.elementBox.getClient(TopoViewEntity.TOPO_VIEW);
//			}
//			return topoView;
//		}
		
		/**
		 * 删除子节点
		 */
		public function removeChildElement(element:Element):void{
			if(element.childrenCount > 0){
				var children:Array = element.children.toArray();
				element.children.clear();
				for each(var child:Element in children){
					//node.removeChild();
					elementBox.remove(child);
				}
			}
		}
		
		/**
		 * 设置自动布局的方式
		 */
		public function setAutoLayouter(index:int , layout:Boolean = true , accountAttachments:Boolean = false):void{
			autoLayouter.accountAttachments = accountAttachments;
			for(var i:int = 0 ; i < autoLayouterData.length ; i++){
				var o:Object =autoLayouterData[i];
				if(Number(o.index) == index){
					if(layout){
						autoLayouter.doLayout(o.value);
					}
					break;
				}
			}
		}
		
		public function elementVisible(state:IElement):Boolean{
//			var visible:Boolean = true;
//			var dataSource:int;
//			if(state is Node){
//				var topoViewNode:TopoViewNode = state.getClient(TopoViewNode.TOPO_POINT) as TopoViewNode;
//				if(null != topoViewNode){
//					dataSource = topoViewNode.topoNode.dataSource;
//				}else{
//					return true
//				}
//			}else if(state is Link){
//				var topoViewLink:TopoViewLink =  state.getClient(TopoViewLink.TOPO_LINE) as TopoViewLink;
//				if(null != topoViewLink){
//					dataSource = topoViewLink.topoLink.dataSource;
//				}else{
//					return true;
//				}
//					
//			}
//			if((dataVisible == 2 && dataSource == 2) ||  (dataVisible == 3  && dataSource == 1) ){
//				visible = false;
//			}
//			
//			return visible;
			return true;
		}	
		
		
		private function handleInteraction(e:InteractionEvent):void{
//			var element:Element = this.selectionModel.lastData as Element;
//			var topoViewData:TopoViewData = new TopoViewData();
//			topoViewData.element = element;
//			topoViewData.network = this;
			switch (e.kind){
				case InteractionEvent.CLICK_ELEMENT:{
//					if(element is Node){
////						this.dispatchEvent(new NetworkEvent(NetworkEvent.NODE_CLICK , topoViewData));
//					}else if(element is Link){
////						this.dispatchEvent(new NetworkEvent(NetworkEvent.LINK_CLICK , topoViewData));
//					}
					break;
				}
				case InteractionEvent.CLICK_BACKGROUND:{
					
					break;
				}
				case InteractionEvent.DOUBLE_CLICK_ELEMENT:{
//					if(element is Node){
//						this.dispatchEvent(new NetworkEvent(NetworkEvent.NODE_DOUBLE_CLICK , topoViewData));
//					}else if(element is Link){
//						this.dispatchEvent(new NetworkEvent(NetworkEvent.LINK_DOUBLE_CLICK , topoViewData));
//					}
					break;
				}
				case  InteractionEvent.LIVE_MOVE_BETWEEN :
				case InteractionEvent.LAZY_MOVE_BETWEEN:{
//					this.dispatchEvent(new NetworkEvent(NetworkEvent.ELEMENT_MOVE_BETWEEN , topoViewData));
					
					break;
				}
				case InteractionEvent.LIVE_MOVE_END :
				case InteractionEvent.LAZY_MOVE_END:{
//					this.dispatchEvent(new NetworkEvent(NetworkEvent.ELEMENT_MOVE_END , topoViewData));
					break;
				}
			}
		}
		
//		private function onEditClickEvent(event:NetworkEvent):void{
//			var elementCreator:ElementCreator = event.data as ElementCreator;
//			if (elementCreator.type == TopoLibConstants.LINK) {
//				var network:Network = this;
//				createLinkInteraction(LinkSubNetwork,elementCreator.shapeType ,function (link:Link):void{
//					setDefaultInteractionHandlers();
//					link = elementCreator.createLink(link , network);
//					network.dispatchEvent(new NetworkEvent(NetworkEvent.LINK_EDIT, link));
//				});
//			}
//         else if (elementCreator.type == TopoLibConstants.NODE) {
//				this.setDefaultInteractionHandlers();
//			}
//		}
		
		/**
		 * 设置network的样式
		 */
		private function setNetwrokStyle():void{
			elementBox.setStyle(Styles.BACKGROUND_TYPE, Consts.BACKGROUND_TYPE_IMAGE);
			elementBox.setStyle(Styles.BACKGROUND_IMAGE_STRETCH, Consts.STRETCH_FILL);
			//	elementBox.setStyle(Styles.BACKGROUND_IMAGE, Constant.NetwokBackGroundImg);
			keyboardRemoveEnabled = false;
			doubleClickToLinkBundle = false;
			doubleClickToEmptySubNetwork = false; 
			doubleClickToUpSubNetwork = false
			doubleClickToSubNetwork = false;
		//	doubleClickToGroupExpand = false;
		}
		
//		private function onDragEnter(evt:DragEvent):void {
//			var data:Object = getData(evt);
//			if(data is IElementCreator||data is IStylesCreator) {
//				var elementCreator:ElementCreator = data as ElementCreator;
//				
//				if(elementCreator.type == TopoLibConstants.NODE ){
//					DragManager.acceptDragDrop(this);
//				}
//				return;
//			}
//		}
		
		private function onDragOver(evt:DragEvent):void{
			
			DragManager.acceptDragDrop(null);
			var data:Object = getData(evt);
//			if(data is IStylesCreator){
//				var link:Link=getElement(evt,Link) as Link;
//				if(link){
//					highlightLink(link);
//				}else{
//					clearLinkHighlight();
//				}
//			}
		}
		
		private function onDragDrop( evt:DragEvent ):void {
			
			var data:Object = getData(evt);
//			if(data is IElementCreator){
//				var element:Element = (data as IElementCreator).createElement(evt,this) as Element;
//				if(element is Node){
//					elementBox.add(element);
//					this.dispatchEvent(new NetworkEvent(NetworkEvent.NODE_EDIT , element as Node ));
//				//	openDetailWidget(element);
//				}
//				
//				return;
//			}
//			if(data is IStylesCreator){
////				var element:IElement=getElement(evt,Link);
////				if(element){
////					(data as IStylesCreator).createStyles(element);
////				}
//			}
//			clearLinkHighlight();
		}
		
		private function getData(evt:DragEvent):Object{
			var data:Object = evt.dragSource.dataForFormat( "items" );
			if( data is Array ) {
				data = data[0];
			}
			return data;
		}
		
		private function createLinkInteraction(linkClass:Class,
											   linkType:String,
											   callback:Function=null,
											   isByControlPoint:Boolean=false,
											   splitByPercent:Boolean=true,
											   value:Number=-1):void{
			this.setCreateLinkInteractionHandlers(linkClass,callback,linkType,isByControlPoint,value,true);
			
		}

		[Bindable]
		/**
		 * 数据的显示
		 * 1:显示自动+手动添加的
		 * 2:显示自动添加的
		 * 3:显示手动添加的
		 * 默认是自动+手动
		 */
		public function get dataVisible():int {
			return _dataVisible;
		}

		/**
		 * @private
		 */
		public function set dataVisible(value:int):void {
			_dataVisible = value;
		}

		
//		/**
//		 * 打开详细信息的页面
//		 */
//		public function openDetailWidget(element:Element):void{
//			var elementData:Object;
//			if(element is Node){
//			//	elementData = BeanContext.getInstance().widgets[ConfigManager.NODE_DETAIL];
//				
//			}
//			if(null != elementData){
//				elementData.element = element;
//				elementData.network = this;
//				NetworkSiteContainer.dispatchEvent(new AppEvent(AppEvent.LOAD_WIDGET,elementData));
//			}
//			
//			
//		}
		
		
		
		
	}
}